package repository;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import model.TermModel;

import entity.BusiestCluster;
import entity.Term;

public class TermRepository {

	/**
	 * Check if is first access.
	 * 
	 * @return
	 * @throws ClassNotFoundException
	 *             invalid driver.
	 * @throws SQLException
	 *             invalid connection.
	 */
	public boolean checkFirstAcess() throws ClassNotFoundException,
			SQLException {

		Integer idCluster = 0;

		Connection connection = FactoryConnection.getInstance().getConnection();

		Statement statement = connection.createStatement();

		ResultSet resultSet = statement
				.executeQuery("SELECT * FROM cluster WHERE id_cluster = 1");

		while (resultSet.next()) {

			idCluster = resultSet.getInt("id_cluster");

		}

		if (idCluster == 1) {

			// not first access.
			return false;

		} else {

			// is first access.
			return true;
		}

	}

	/**
	 * 
	 * Get all clusters busiest
	 * 
	 * @return
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 */
	public List<BusiestCluster> getClustersBusiest()
			throws ClassNotFoundException, SQLException {

		// Open connection
		Connection connection = FactoryConnection.getInstance().getConnection();

		Statement statement = connection.createStatement();

		ResultSet resultClusters = statement
				.executeQuery("SELECT * FROM busiest_cluster order by count desc");

		List<BusiestCluster> busiestClusters = new ArrayList<>();

		// Retrieve all clusters busiest
		while (resultClusters.next()) {

			BusiestCluster busiestCluster = new BusiestCluster();

			busiestCluster.setIdCluster(resultClusters
					.getInt("id_busiest_cluster"));

			busiestCluster.setId(resultClusters.getInt("cluster_id_cluster"));

			busiestCluster.setCount(resultClusters.getInt("count"));

			busiestClusters.add(busiestCluster);

		}

		return busiestClusters;

	}

	/**
	 * get all term of a particula cluster.
	 * 
	 * @param idCluster
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */
	public List<Term> getTerms(int idCluster) throws ClassNotFoundException,
			SQLException {

		List<Term> terms = new ArrayList<>();

		// Open connection
		Connection connection = FactoryConnection.getInstance().getConnection();

		Statement statement = connection.createStatement();

		ResultSet resultClusters = statement.executeQuery("SELECT * FROM term WHERE cluster_id_cluster = "+ idCluster + "");

		while (resultClusters.next()) {

			Term term = new Term(resultClusters.getInt("id_term"),resultClusters.getInt("cluster_id_cluster"),resultClusters.getString("term"),resultClusters.getString("new_term"));
			terms.add(term);

		}

		return terms;
	}

	public void saveTerm(List<Term> termsParam) throws ClassNotFoundException,
			SQLException {

		List<BusiestCluster> idClusters = getClustersBusiest();

		Connection connection = FactoryConnection.getInstance().getConnection();

		Statement statement = connection.createStatement();
		
		TermModel termModel = new TermModel();
		
		boolean isIdentical = false;
		
		if (checkFirstAcess()) {

			// consulta ask

		} else {

			// get all busiest cluster
			for (BusiestCluster busiestCluster : idClusters) {

				List<Term> termsRepository = getTerms(busiestCluster.getId());
				
				double score = termModel.checkSimilarQuery(termsParam, termsRepository);
				
				
				
				if(score >= 1.000){
					
					// get all term repositories
					for (Term termRepository : termsRepository) {

						// get all term of a param
						for (Term termParam : termsParam) {

								
							if (!termParam.getTerm().equals(termRepository.getTerm())) {
								
								statement.executeUpdate("INSERT INTO term (cluster_id_cluster, term, new_term) VALUES(" +busiestCluster.getId()+ ",'"+termParam.getTerm()+"','"+termParam.getTerm()+"');");
								
								break;
							}
														
						}
						
					}
				
				} else if (score >= 0.600){
					
					for (Term termParam : termsParam) {

						// get all term of a param
						for (Term  termRepository : termsRepository ) {

								
							if (termParam.getTerm().equals(termRepository.getTerm())) {
								
								isIdentical = true;
								break;
							}else{
								
								isIdentical=false;
							}
														
						}
						
						if(isIdentical == false){
							
							statement.executeUpdate("INSERT INTO term (cluster_id_cluster, term, new_term) VALUES(" +busiestCluster.getId()+ ",'"+termParam.getTerm()+"','"+termParam.getTerm()+"');");
						}
					}

				}else{
					
					
					// consulta ask
					//criar um cluster
				}
				
				long count = busiestCluster.getCount()+1;
				//Atualiza a tabela de cluster mais acessado.
				statement.executeUpdate("UPDATE busiest_cluster SET cluster_id_cluster="+busiestCluster.getIdCluster()+", count="+count+" WHERE id_busiest_Cluster="+busiestCluster.getId()+"");
				
			}

		}

	}
	
	
	public BusiestCluster getBusiestCluster(int idBusiestCluster) throws ClassNotFoundException, SQLException{
		
		Connection connection = FactoryConnection.getInstance().getConnection();

		Statement statement = connection.createStatement();


		ResultSet result = statement.executeQuery("SELECT * FROM busiest_cluster WHERE id_busiest_cluster = "+ idBusiestCluster + "");
		
		while(result.next()){
			
			return  new BusiestCluster(result.getInt("id_busiest_cluster"),result.getInt("cluster_id_cluster"),result.getInt("count"));
			
		}
		
		return null;
	}
}
